package context;

import java.io.File;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.yaml.snakeyaml.Yaml;

import config.mm_config;

/**
 * 配置文件守护，发现配置文件更改。在稍后将会更新内存中的配置文件
 * @author dongyazhuo
 */
public class mm_config_guard implements Runnable {

	private String TAG = mm_context.class.getSimpleName() + " ";

	@Override
	public void run() {
		// TODO!!!
		// query_or_update();
	}

	private void query_or_update() {
		mm_context _mm_context = mm_context.instance();
		Yaml yaml = new Yaml();

		File[] tempList = new File("src/main/resources/ticket_config/").listFiles();
		for (int i = 0; i < tempList.length; i++) {
			if (tempList[i].isFile()) {
				String resource = "/ticket_config/" + tempList[i].getName();
				mm_config config = yaml.loadAs(mm_config.class.getResourceAsStream(resource), mm_config.class);
				config.setUrl_config(get_url_config(yaml));
				LinkedHashMap<String, mm_config> memory = _mm_context.get_configs();
				Set<Entry<String, mm_config>> entrySet = memory.entrySet();
				for (Entry<String, mm_config> entry : entrySet) {

				}
			}
		}
	}

	private LinkedHashMap<String, Map<String, Object>> get_url_config(Yaml yaml) {
		// 如果读入Map,这里可以是Mapj接口,默认实现为LinkedHashMap
		InputStream inputStream = mm_config.class.getResourceAsStream("/url_config.yml");
		LinkedHashMap<String, Map<String, Object>> url_config = yaml.loadAs(inputStream, LinkedHashMap.class);
		return url_config;
	}

}
